package shoppingmall.reception.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import shoppingmall.reception.dto.LoginRequestParamsVO;
import shoppingmall.reception.mapper.SignInMapper;
import shoppingmall.reception.pojo.User;
import shoppingmall.reception.service.SignInService;

/**
 * 服务层接口实现类，平台会员用户登录相关
 */
@Service
public class SignInServiceImpl implements SignInService {
    //会员登录mapper层实例
    @Autowired
    private SignInMapper signInMapper;

    @Override
    public User userLogin(LoginRequestParamsVO user) {
        //根据用户名或手机号码 + 密码组合进行判断
        User userInfo = signInMapper.getUserInfoByUserName(user.getUsername());
        if(userInfo != null) {
            boolean passwordIsSame = userInfo.getPassword().equals(user.getPassword());
            if(passwordIsSame) {
                return userInfo;
            } else {
                return null;
            }
        } else if (userInfo == null) {
            userInfo = signInMapper.getUserInfoByUserPhone(user.getPhone());
            //此处可能抛异常，用户名 及 手机号都未查询到用户，进行捕获异常
            try {
                boolean passwordIsSame = userInfo.getPassword().equals(user.getPassword());
                if(passwordIsSame) {
                    return userInfo;
                } else {
                    return null;
                }
            } catch (Exception e) {
                return null;
            }
        }
        return null;
    }
}
